import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'
const instance = axios.create({
    baseURL: '/api',
    timeout: 5000,
});


// Add a request interceptor
instance.interceptors.request.use(function (config) {
    // Do something before request is sent
    // console.log(config);
    if (store.state.user.token) {
        config.headers.token = store.state.user.token
    }
    NProgress.start();
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor
instance.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    NProgress.done();
    return response.data;
}, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    // return Promise.reject(error);
    console.log(error.message)
    NProgress.done();
    return new Promise(() => { });
});

export default instance